﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Hjp.FrameWork.Common;
using Wjtour.Domain;
using Wjtour.BLL;

namespace Wjtour.Web.Admin.article_type
{
    public partial class addedi : BasePage
    {
        protected TextBox title;
        protected DropDownList pid;
        protected HiddenField tag;
        protected TextBox sort;
        protected Button submit;
        protected int id = 0;
        protected string doing;
        protected string type;

        Wjtour.Domain.articletype link = new Domain.articletype();
        articletype_bll _bll = new articletype_bll();
        articletype_bll _tbll = new articletype_bll();

        protected void Page_Load(object sender, EventArgs e)
        {
            id = Utils.StrToInt(Request["id"], 0);
            doing = Request["do"];
            type = Request["type"];

            submit.Click += new EventHandler(submit_Click);
            if (!IsPostBack)
            {
                BindDropdown();
                if (id > 0)
                {
                    link = _bll.Load(id);
                    if (link != null)
                    {
                        if (doing == "edi")
                        {
                            title.Text = link.title;
                            tag.Value = link.tag;
                            pid.SelectedValue = link.pid.ToString();
                            sort.Text = link.sort.ToString();
                        }
                        if (doing == "add")
                        {
                            pid.SelectedValue = link.id.ToString();
                            tag.Value = "article";
                            title.Text = "";
                            sort.Text = "0";
                        }
                    }
                }
            }
        }

        void BindDropdown()
        {
            string sql = "select * from articletype";
            if (!string.IsNullOrEmpty(type))
                sql = "select * from articletype where id in (" + type + ")";

            List<articletype> types = _tbll.GetAll<articletype>(sql).ToList();
            if (types != null && types.Count > 0)
            {
                List<articletype> ptypes = types.Where(t => t.pid == 0).ToList();
                foreach (var ptype in ptypes)
                {
                    pid.Items.Add(new ListItem(ptype.title, ptype.id.ToString()));
                    List<articletype> subtypes = types.Where(t => t.pid == ptype.id).ToList();
                    if (subtypes != null && subtypes.Count > 0)
                    {
                        foreach (var item in subtypes)
                        {
                            pid.Items.Add(new ListItem(" ┝ " + item.title, item.id.ToString()));
                        }
                    }
                }
            }
        }

        void submit_Click(object sender, EventArgs e)
        {
            link.title = title.Text;
            link.tag = tag.Value;
            link.pid = Utils.StrToInt(pid.SelectedValue, 0);
            if (doing == "edi")
            {
                link.id = id;
            }
            link.sort = Utils.StrToInt(sort.Text, 0);
            if (link.id == 0)
                link.addtime = DateTime.Now;

            bool b = _bll.Save(link);
            if (b)
            {
                SaveLog(id == 0 ? "Add" : "edit");
                Utils.Alert(this.Page, "保存成功！");
                Utils.Redirect(this.Page, "list.aspx", false);
            }
        }
    }
}